//
//  RequestURl.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "RequestURl.h"
#import "CommonModel.h"
#import "PicModel.h"
#import "VideoModel.h"
#import "ScrollPicModel.h"

@implementation RequestURl

- (void)requestWithUrlID:(NSInteger )urlID
                 UrlPage:(NSInteger )urlPage
                 success:(success)success
                    fail:(fail)fail {
    
    NSMutableArray *arrayAllData = [NSMutableArray array];
    NSMutableArray *arrayPicData = [NSMutableArray array];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%ld%@%ld%@",kFirstUrl,urlID,kSecendUrl,urlPage,kThirdUrl];
    
    
    // 转码
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:urlStr];
    NSString *urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            if (dic.allKeys.count == 0) {
                NSError *err = [NSError errorWithDomain:@"请求失败" code:0 userInfo:nil];
                dispatch_async(dispatch_get_main_queue(), ^{
                    fail(err);
                    return ;
                });
            }
            
            if ((urlID == 0 || urlID == 210 || urlID == 74 || urlID == 300 || urlID == 165) && [dic.allKeys containsObject:@"pics"]) {
                for (NSDictionary *dicTem in dic[@"pics"]) {
                    ScrollPicModel *scrollPM = [[ScrollPicModel alloc] init];
                    [scrollPM setValuesForKeysWithDictionary:dicTem];
                    [arrayPicData addObject:scrollPM];
                }
            }
            
            
            for (NSDictionary *dicTem in dic[@"list"]) {
                
                if ([dicTem.allKeys containsObject:@"pics"] == YES) {

#warning type= 5（直播）imgsrc为空，实际是显示pics中的图片，初步处理为简单的赋值
                    if ([dicTem[@"pics"] count] == 1) {
                        CommonModel *commonM = [[CommonModel alloc] init];
                        [commonM setValuesForKeysWithDictionary:dicTem];
                        commonM.imgsrc = dicTem[@"pics"][0];
                        [arrayAllData addObject:commonM];
                    } else {
                        PicModel *picM = [[PicModel alloc] init];
                        [picM setValuesForKeysWithDictionary:dicTem];
                        [arrayAllData addObject:picM];
                    }
                }
                else if ([dicTem[@"type"] integerValue] == 9) {
                    VideoModel *videoM = [[VideoModel alloc] init];
                    [videoM setValuesForKeysWithDictionary:dicTem];
                    [arrayAllData addObject:videoM];
                }
                
                else {
                    CommonModel *commonM = [[CommonModel alloc] init];
                    [commonM setValuesForKeysWithDictionary:dicTem];
                    [arrayAllData addObject:commonM];
                }
            }
            
            if (success) {
                    success(arrayAllData,arrayPicData);
            }
        }
    }];

    [task resume];
    
}


@end
